package org.im.service.websocket.server;

import java.util.Collection;

import javax.websocket.EndpointConfig;
import javax.websocket.OnClose;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;

import org.im.service.message.bean.ActionType;
import org.im.service.message.bean.MessageType;
import org.im.service.message.bean.SenderType;
import org.im.service.message.exp.MessageException;
import org.im.service.message.util.MessageUtil;
import org.im.service.message.webmsg.UserInfoMessage;
import org.im.service.websocket.server.pool.WebsocketSessionPool;

import com.alibaba.fastjson.JSON;

@ServerEndpoint(value = "/websocket")
public class WebsocketPipelineAction {

    @OnOpen
    public void open(Session session, EndpointConfig config) {
        Collection<String> ids = WebsocketSessionPool.getAllSessionIds();

        // 新会话
        String sessionId = session.getId();
        WebsocketMessageContext.createWebsocketPipeline(session);
        // 发送给自己的用户信息
        UserInfoMessage userInfo = new UserInfoMessage(MessageUtil.SYSTEM_SENDER_ID, sessionId);
        // 属于系统发送的消息
        userInfo.setSenderType(SenderType.SYSTEM.getType());
        // 系统回馈消息
        userInfo.setMessageType(MessageType.SYSTEM_ECHO.getType());
        // 用户信息的sessionId
        userInfo.setSessionId(sessionId);
        // 现有的用户列表，后期改为好友列表
        userInfo.setOtherSessionIds(ids);
        try {
            WebsocketMessageContext.sendMessage(session, sessionId, JSON.toJSONString(userInfo));
        } catch (MessageException e1) {
            e1.printStackTrace();
        }

        // 发送系统消息：向好友发送登录消息
        try {

            userInfo = new UserInfoMessage(MessageUtil.SYSTEM_SENDER_ID, sessionId);
            userInfo.setSenderType(SenderType.SYSTEM.getType());
            userInfo.setActionType(ActionType.NEW_USER.getType());
            // 用户触发给其他用户的消息
            userInfo.setMessageType(MessageType.SYSTEM_BROADCAST.getType());
            userInfo.setSessionId(sessionId);

            WebsocketMessageContext.sendMessageToSession(session, ids, JSON.toJSONString(userInfo));
        } catch (Exception e) {
        }
    }

    @OnMessage
    public void inMessage(Session session, String message) {
        // 接收到客户端消息，发送给指定的session
        try {
            WebsocketMessageContext.sendMessageToSession(session, message);
        } catch (MessageException e) {
            e.printStackTrace();
        }
    }

    @OnClose
    public void end(Session session) {

        try {
            Collection<String> toIds = WebsocketSessionPool.getAllSessionIds();
            UserInfoMessage userInfo = new UserInfoMessage(MessageUtil.SYSTEM_SENDER_ID, session.getId());
            userInfo.setSenderType(SenderType.SYSTEM.getType());
            userInfo.setActionType(ActionType.USER_LEAVE.getType());
            // 用户触发给其他用户的消息
            userInfo.setMessageType(MessageType.SYSTEM_BROADCAST.getType());
            userInfo.setSessionId(session.getId());
            toIds.remove(session.getId());
            WebsocketMessageContext.sendMessageToSession(session, toIds, JSON.toJSONString(userInfo));
        } catch (Exception e) {
            e.printStackTrace();
        }

        // 会话结束
        WebsocketMessageContext.distroyWebsocketPipeline(session);
    }
}
